package com.youdu.fragment.home;

import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.youdu.R;
import com.youdu.adapter.CourseAdapter;
import com.youdu.fragment.BaseFragment;
import com.youdu.fragment.views.HomeHeaderLayout;
import com.youdu.module.recommand.BaseRecommandModel;
import com.youdu.netwrok.http.RequestJsonCenter;


public class HomeFragment extends BaseFragment implements View.OnClickListener, AdapterView.OnItemClickListener {

    private static final String TAG = "HomeFragment";
    private View mContentView;
    private ListView mListView;
    private TextView mQRCodeView;
    private TextView mCategoryView;
    private TextView mSearchView;
    private ImageView mLoadingView;

    private CourseAdapter mAdapter;
    private BaseRecommandModel mRecommandData;

    public HomeFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mContext = getActivity();
        mContentView = inflater.inflate(R.layout.fragment_home_layout, container, false);
        return mContentView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        requestRecommandData();
    }

    //首页的数据请求
    private void requestRecommandData() {
        mLoadingView.setVisibility(View.GONE);
        mRecommandData = (BaseRecommandModel) RequestJsonCenter.getJson("home_data.json",  BaseRecommandModel.class);
        mListView.setVisibility(View.VISIBLE);
        //为listview添加头
        mListView.addHeaderView(new HomeHeaderLayout(mContext, mRecommandData.data.head));

        mAdapter = new CourseAdapter(mContext,mRecommandData.data.list);
        mListView.setAdapter(mAdapter);


//        Log.e("error", "----------------"+data);
//        RequestCenter.requestRecommandData(new DisposeDataListener() {
//            @Override
//            public void onSuccess(Object responseObj) {
//                Log.e(TAG,"成功打印="+responseObj);
//            }
//
//            @Override
//            public void onFailure(Object reasonObj) {
//                Log.e(TAG,"成功失败="+reasonObj);
//            }
//        });
    }

    private void initView() {
        mQRCodeView = (TextView) mContentView.findViewById(R.id.qrcode_view);
        mQRCodeView.setOnClickListener(this);
        mCategoryView = (TextView) mContentView.findViewById(R.id.category_view);
        mCategoryView.setOnClickListener(this);
        mSearchView = (TextView) mContentView.findViewById(R.id.search_view);
        mSearchView.setOnClickListener(this);
        mListView = (ListView) mContentView.findViewById(R.id.list_view);
        mListView.setOnItemClickListener(this);
        mLoadingView = (ImageView) mContentView.findViewById(R.id.loading_view);
        AnimationDrawable anim = (AnimationDrawable) mLoadingView.getDrawable();
        anim.start();
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }
}